#!/usr/bin/env python
#coding: utf-8
'''
Created on 2017年5月18日

@author: Administrator
'''
from account import settings
from account.models import Profile
from account.tasks import update_user_to_es_task
from django.contrib.auth.models import User
import logging

# 为loggers中定义的名称
logger = logging.getLogger("celery")

def update_user_to_es(users):
    '''
    
    '''
    #判断users类型
    if isinstance(users, User):
        users = [users]
        
    for user in users:
        #构造文章json字符串
        logger.info(u"start make user es source by id %s " % user.id)    
        _source = {
            "id":user.id,
            "last_login" : user.last_login.strftime("%Y-%m-%d %H:%M:%S"),
            "date_joined" : user.date_joined.strftime("%Y-%m-%d %H:%M:%S"),
            "is_superuser" : user.is_superuser,
            "username" : user.username,
            "first_name" : user.first_name,
            "last_name" : user.last_name,
            "email" : user.email,
            "is_staff" : user.is_staff,
            "is_active" : user.is_active,
            "create_time":"",
            "modify_time":"",
            "state":"",
            "utype":"",
            "sex":"",
            "mobile":"",
            "identity_no":"",
            "wechat":"",
            "qq":"",
            "weibo":"",
            "email_authed":"",
            "mobile_authed":"",
            "identity_authed":"",
            "address":"",
            "avatar_fpath":"",
            "origin_avatar_fpath":"",
            "intro":"",
            "avatar_source_url":"",
        }
        try:
            _source["create_time"] = user.profile.create_time.strftime("%Y-%m-%d %H:%M:%S")
            _source["modify_time"] = user.profile.modify_time.strftime("%Y-%m-%d %H:%M:%S")
            _source["state"] = user.profile.state
            _source["state_desc"] = settings.get_state_desc(user.profile.state)
            _source["utype"] = user.profile.utype
            _source["utype_desc"] = settings.USER_TYPE_CHOICES[user.profile.utype][1]
            _source["sex"] = user.profile.sex
            _source["sex_desc"] = settings.get_user_sex_desc(user.profile.sex)
            _source["mobile"] = user.profile.mobile
            _source["identity_no"] = user.profile.identity_no
            _source["wechat"] = user.profile.wechat
            _source["qq"] = user.profile.qq
            _source["weibo"] = user.profile.weibo
            _source["email_authed"] = user.profile.email_authed
            _source["mobile_authed"] = user.profile.mobile_authed
            _source["identity_authed"] = user.profile.identity_authed
            _source["address"] = user.profile.address
            _source["avatar_fpath"] = user.profile.avatar_fpath
            _source["origin_avatar_fpath"] = user.profile.origin_avatar_fpath
            _source["intro"] = user.profile.intro
            _source["avatar_source_url"] = user.profile.avatar_source_url
        except Profile.DoesNotExist:
            pass

        update_user_to_es_task.delay(_source)
        logger.info(u"end make user es source by id %s " % user.id)    
